<?php
class TdRenderer extends TdAbstractRenderer {
  public $container = null;
  public $template;
  public $arguments = array();
  public $components = array();
  public $errormessage = "";

  private function parse_template($template) {
    foreach (get_class_vars(get_class($this)) as $varname => $vardefault) {
      $$varname = $this->$varname;
    }

    foreach ($this->arguments  as $varname => $vardefault) {
      $$varname = $vardefault;
    }

    foreach ($this->components as $id => $obj) {
      $$id = $obj;
    }

    ob_start();
    require($template);
    return ob_get_clean();
  }

  function __construct($template) {
    global $teardrop;
    
    $this->arguments = $teardrop->arguments;
    $this->template = $template;    
    
  }

  function doCheck() {
    foreach ($this->components as $id => $obj) {
      $obj->doCheck();
    }
  }

  function doInit() {
    foreach ($this->components as $id => $obj) {
      $obj->doInit();
    }
  }
  
  function doValidate() {
  	foreach ($this->components as $id => $obj) {
  		$obj->doValidate();
  	}
  }

  function addComponent($obj) {
  	/*
    if (isset($_SESSION[$obj->id])) {
      $this->components[$obj->id] = $_SESSION[$obj->id];
    }
    else {
      $this->components[$obj->id] = $obj;
      $_SESSION[$obj->id] = $this->components[$obj->id];
    }
    */
    $this->components[$obj->id] = $obj;
  }

  function getArguments() {
    return $this->arguments;
  }

  function getArgument($argname) {
    return $this->arguments[$argname];
  }
  

  function __toString() {
  	/* deprecated
    if (isset($this->components[$_SERVER['QUERY_STRING']])) {
      return $this->components[$_SERVER['QUERY_STRING']];
    } else */
    
    if (isset($this->template) && is_file($this->template)) {
      return chop($this->parse_template($this->template));
    } else {
      return "";
    }
  }
}
?>